#coding=utf-8
'''
2.2. Matching Text at the Start or End of a String
starswith(), endswith()
'''
filename = 'spam.txt'
a = filename.endswith('.txt')
b = filename.startswith('file:')
print(a)
print(b)
url = 'http://www.python.org'
c = url.startswith('http:')
print(c)

import os
filenames = os.listdir('.')
print(filenames)
d = [name for name in filenames if name.endswith('.py')]
print(d)
e = any(name.endswith('.py') for name in filenames)
print(e)

from urllib.request import urlopen
def read_data(name):
    if name.starswith(('http:', 'https:', 'ftp:')):
        return urlopen(name).read()
    else:
        with open(name) as f:
            return f.read()

choices = ['http:', 'ftp:']
url = 'http://www.python.org'
# url.startswith(choices)
url.startswith(tuple(choices))

import re
url = 'http://www.python.org'
d = re.match('http:|https:|ftp:', url)
print(d)